'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 签到
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var modelUtil = require('../util/modelUtil');
var _ = require('lodash');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var checkSchema = require('../schemas/checkSchema');
var Check = mongoConnector().model('Check');

var checkModel = module.exports;

checkModel.getByRoleId = function (role_id, cb) {
    modelUtil.getByRoleId(Check, role_id, function (err, check_model) {
        if (!!err) {
            console.error(err);
            return cb(err);
        }

        cb(null, check_model);
    });
};

/**
 * 0点刷新
 */
module.exports.onDailyRefresh = function (self, cb) {
    _resetCheck(self);
    cb();
};

/**
 * 每月重置签到信息
 */
var _resetCheck = function (check_model) {
    if (moment(check_model.getLastRefreshTime()) <= moment().startOf('month')) {
        check_model.resetCheckInfo();
    }
};
